package cz.cvut.manatee.processManager;

import com.phrasebox.manatee.Concordance;
import com.phrasebox.manatee.Corpus;
import com.phrasebox.manatee.CollocItems;

public class CollocationQuery extends ContainsCQLQuery {
	/*
	 * promene ktere se predavaji strukture CollocItems
	 */
	protected final String 	attr_name;		// typ hledané formy (word, lemma, tag, mluvnické formy)
	protected final char 	sort_fun_code;	// podle čeho řadit výsledky ("t" - T-score, "m" - MI, "3" - MI3, "l" - log likelihood, "s" - min. sensitivity, "c" - salience)
	protected final long 	minfreq;		// minimální frekvence slov v korpusu
	protected final long 	minbgr;			// minimální frekvence slov v nalezeném výsledku
	protected final int 	fromw;			// počet slov v pravém kontextu
	protected final int 	tow;			// počet slov v levém kontextu
	protected final int 	maxitems;		// počet hledaných položek 
	
	public CollocationQuery(String corpusName, String CQL, String attr_name, char sort_fun_code, long minfreq, long minbgr, int fromw, int tow, int maxitems) {
		super(corpusName, CQL);
		this.attr_name 		= attr_name;
		this.sort_fun_code 	= sort_fun_code;
		this.minfreq 		= minfreq;
		this.minbgr 		= minbgr;
		this.fromw 			= fromw;
		this.tow 			= tow;
		this.maxitems 		= maxitems;
	}
	
	public void execute(ThreadRunnable thread) {
		//zavolat zmenu stavu na executing

		Logging.get().info("Query: execute na "+this+" pocitano na "+thread);
		
		Corpus corp = OptimizedCorpus.getInstance(thread, this.corpusName);
		
		Concordance conc = OptimizedConcordance.getInstance(thread, corp, this.CQL);

		// priklad CollocItems coll = new CollocItems (conc, "word", 'f', 1,1, -5, 5, 20);
		CollocItems coll = new CollocItems (conc, this.attr_name, this.sort_fun_code, this.minfreq, this.minbgr, this.fromw, this.tow, this.maxitems);
		
		CollocationResult result = new CollocationResult();

		while (!coll.eos()) {
			result.addLine();
			result.addWord(coll.get_item());
			result.addWord(""+coll.get_freq());
			result.addWord(""+coll.get_cnt());
		    coll.next();
		}
		
		this.result = result;
	}
	
	/*
	 * Vypisuje svuj obsah
	 */
	public String toString() {
		return "CollocationQuery("+super.toString()+")";
	}
}
